<template>
  <div>
    <div class="page-form">
      <el-input
        v-model="queryParams.examScanDetails.batchNo"
        placeholder="批次"
        clearable
        @input="handleSearch"
      />
      <el-select
        v-model="queryParams.examScanDetails.examRoomNo"
        placeholder="选择考场"
        clearable
        @change="handleSearch"
      >
        <el-option
          v-for="item in kclb"
          :key="item.examRoomNo"
          :label="item.examRoomNo"
          :value="item.examRoomNo"
        ></el-option>
      </el-select>
      <el-select
        v-model="queryParams.examScanDetails.classId"
        placeholder="选择班级"
        clearable
        @change="handleSearch"
      >
        <el-option
          v-for="item in bjlb"
          :key="item.id"
          :label="item.className"
          :value="item.id"
        ></el-option>
      </el-select>
      <el-input
        v-model="queryParams.examScanDetails.admissionNo"
        placeholder="准考证号"
        clearable
        @input="handleSearch"
      />
      <el-input
        v-model="queryParams.examScanDetails.studentName"
        placeholder="姓名"
        clearable
        @input="handleSearch"
      />
      <el-select
        v-model="queryParams.examScanDetails.releasePassCard"
        placeholder="是否已释放"
        clearable
        @change="handleSearch"
      >
        <el-option label="已释放" value="1"></el-option>
        <el-option label="未释放" value="0"></el-option>
      </el-select>
    </div>
    <el-table
      stripe
      border
      ref="tableList"
      v-loading="loading"
      :data="tableData"
      style="width: 100%"
      row-key="id"
      @selection-change="handleSelectionChange"
      v-show="showTable"
    >
      <el-table-column
        type="selection"
        width="55"
        align="center"
        :reserve-selection="true"
      />
      <el-table-column
        label="批次号"
        align="center"
        prop="batchNo"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        label="流水号"
        align="center"
        prop="serialNo"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        label="姓名"
        align="center"
        prop="studentName"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        label="准考证号"
        align="center"
        prop="admissionNo"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        label="班级"
        align="center"
        prop="examClassName"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        label="考场号"
        align="center"
        prop="examRoomNo"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        label="座号"
        align="center"
        prop="seatNo"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        label="异常状态"
        align="center"
        :show-overflow-tooltip="true"
        width="160"
      >
        <template slot-scope="scope">
          <el-tag
            type="danger"
            size="mini"
            v-if="scope.row.abnormalOption == '1' && scope.row.missingExam != 1"
            >选项异常</el-tag
          >
          <el-tag size="mini" v-if="scope.row.passCard == '1'">后传题卡</el-tag>
        </template>
      </el-table-column>
      <el-table-column
        label="上传时间"
        align="center"
        :show-overflow-tooltip="true"
      >
        <template slot-scope="scope">
          {{ parseTime(scope.row.createTime) }}
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        align="center"
        width="260"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-link
            v-hasRole="['admin']"
            type="primary"
            v-if="scope.row.releasePassCard == '0'"
            @click="sf(scope.row.id)"
            >释放题卡</el-link
          >
          <el-link type="success" v-else :underline="false">已释放</el-link>
          <el-link type="primary" @click="cktk(scope.row)">查看题卡</el-link>
          <el-link
            v-hasRole="['admin']"
            type="warning"
            v-if="scope.row.missingExam == 1"
            @click="qk(scope.row.id, 0)"
            >取消缺考</el-link
          >
          <el-link
            v-hasRole="['admin']"
            type="warning"
            v-if="scope.row.missingExam == 0"
            @click="qk(scope.row.id, 1)"
            >缺考</el-link
          >
          <el-link type="danger" @click="del(scope.row.id)">删除</el-link>
        </template>
      </el-table-column>
    </el-table>
    <div class="img-list" v-show="!showTable">
      <el-checkbox-group class="list" v-model="ids" @change="checkChange">
        <div v-for="item in tableData" :key="item.id">
          <el-checkbox :label="item.id">{{ item.studentName }}</el-checkbox>
          <div class="img-item" @click="cktk(item)">
            <img :src="item.minFrontImgPath" alt="" />
            <img :src="item.minBackImgPath" alt="" />
          </div>
        </div>
      </el-checkbox-group>
    </div>
    <div class="page-bottom">
      <div class="sel-box">
        <p>
          已选 <span>{{ ids.length }}</span> 项 / 共 {{ total }} 项
        </p>
        <el-button
          type="primary"
          v-if="$route.query.examSchoolId"
          @click="dc('/scan/notuploaded/record/export')"
          >导出未上传名单</el-button
        >
        <el-button
          type="primary"
          v-if="$route.query.examSchoolId"
          @click="dc('/scan/misscard/record/export')"
          >导出缺考名单</el-button
        >
        <el-button
          v-hasRole="['admin']"
          type="primary"
          :disabled="multiple"
          @click="sf(false)"
          >批量释放</el-button
        >
        <el-button
          v-hasRole="['admin']"
          type="primary"
          :disabled="total == 0"
          @click="qbsf"
          >全部释放</el-button
        >
        <el-button type="primary" @click="showTable = !showTable">{{
          showTable ? "图片模式" : "列表模式"
        }}</el-button>
        <el-button
          v-hasRole="['admin']"
          type="danger"
          :disabled="multiple"
          @click="del(false)"
          >批量删除</el-button
        >
      </div>

      <pagination
        v-show="total > 0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
      />
    </div>

    <el-image-viewer
      v-if="imgViewerVisible"
      :on-close="closeImgViewer"
      :url-list="imgList"
    />
  </div>
</template>

<script>
import {
  scanRecognitionRecord,
  scanAbsence,
  scanDel,
  scanRelPasscardAll,
} from "@/api/xmgl/smgl";
import { debounce } from "lodash";
export default {
  components: {
    "el-image-viewer": () =>
      import("element-ui/packages/image/src/image-viewer"),
  },
  props: {
    xmxx: {},
    examSchoolId: {},
    examSubjectId: {},
    bjlb: null,
    kclb: null,
  },
  data() {
    return {
      total: 0,
      ids: [],
      tableData: [],
      multiple: true,
      loading: false,
      queryParams: {
        pageNum: 1,
        pageSize: 15,
        examScanDetails: {
          batchNo: "",
          examRoomNo: "",
          classId: "",
          admissionNo: "",
          studentName: "",
          releasePassCard: "",
          passCard: 1,
        },
      },

      imgViewerVisible: false,
      imgList: [],
      showTable: true,
    };
  },
  methods: {
    debouncedSearch: debounce(function () {
      this.queryParams.pageNum = 1;
      this.getList();
    }, 500),
    handleSearch() {
      this.debouncedSearch();
    },
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.multiple = !selection.length;
    },
    getList(page = null) {
      if (page) {
        this.queryParams.pageNum = page;
      }
      this.loading = true;
      this.tableData = [];

      scanRecognitionRecord({
        ...this.queryParams,
        examId: this.xmxx.id,
        examSubjectId: this.examSubjectId,
        examSchoolId: this.examSchoolId ? [this.examSchoolId] : null,
      })
        .then((response) => {
          this.total = response.data.total;
          this.tableData = response.data.records;
        })
        .finally(() => {
          this.loading = false;
        });
    },
    qk(id, missingExam) {
      this.$modal
        .confirm(`请确认进行${missingExam ? "取消" : ""}缺考操作`)
        .then(() => {
          scanAbsence({
            ids: id ? [id] : this.ids,
            missingExam: missingExam,
            type: 2,
          }).then((res) => {
            this.$modal.msgSuccess("操作成功");
            this.$refs.tableList.clearSelection();
            this.ids = [];
            this.getList();
          });
        });
    },
    del(id) {
      this.$modal.confirm("请确认进行删除操作").then(() => {
        scanDel({ ids: id ? [id] : this.ids }).then((res) => {
          this.$modal.msgSuccess("操作成功");
          this.$refs.tableList.clearSelection();
          this.ids = [];
          this.getList();
        });
      });
    },
    sf(id) {
      this.$modal.confirm("请确认进行释放题卡操作").then(() => {
        scanAbsence({
          ids: id ? [id] : this.ids,
          type: 1,
          releasePassCard: 1,
        }).then((res) => {
          this.$modal.msgSuccess("操作成功");
          this.$refs.tableList.clearSelection();
          this.ids = [];
          this.getList();
        });
      });
    },
    qbsf() {
      this.$modal.confirm("请确认进行全部释放题卡操作").then(() => {
        scanRelPasscardAll({
          examScanDetails: {
            ...this.queryParams.examScanDetails,
          },
          examId: this.xmxx.id,
          examSubjectId: this.examSubjectId,
          examSchoolId: this.examSchoolId ? [this.examSchoolId] : null,
        }).then((res) => {
          this.$modal.msgSuccess("操作成功");
          this.$refs.tableList.clearSelection();
          this.ids = [];
          this.queryParams.pageNum = 1;
          this.getList();
        });
      });
    },

    cktk(row) {
      this.imgList = [row.frontImgPath, row.backImgPath];
      this.imgViewerVisible = true;
      const m = (e) => {
        e.preventDefault();
      };
      document.body.style.overflow = "hidden";
      document.addEventListener("touchmove", m, false); // 禁止页面滑动
    },
    closeImgViewer() {
      this.imgViewerVisible = false;
      const m = (e) => {
        e.preventDefault();
      };
      document.body.style.overflow = "auto";
      document.removeEventListener("touchmove", m, true);
    },
    dc(url) {
      this.download(
        url,
        {
          examId: this.xmxx.id,
          examSchoolId: this.$route.query.examSchoolId,
        },
        ``
      );
    },
    checkChange(value) {
      this.tableData.map((item) => {
        if (value.indexOf(item.id) != -1) {
          this.$refs.tableList.toggleRowSelection(item, true);
        } else {
          this.$refs.tableList.toggleRowSelection(item, false);
        }
      });
    },
  },
};
</script>

<style lang="scss" scoped>
.img-list > .list {
  width: 100%;
  display: flex;
  flex-wrap: wrap;

  .img-item {
    margin: 0 15px 15px 15px;
    cursor: pointer;

    img {
      display: block;
      width: 360px;
    }
  }
}
</style>
